﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using CP.Win.IViews.GPM;
using CP.Controllers.GPM;
using CP.Entities;

using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraEditors;

namespace GPM
{
    public partial class FrmColListAuthorize : BaseForm, IListAuthorizeView
    {
        public int CurrentGroupID { get; private set; }
        public int CurrentWebpartID { get; private set; } 
        public ListAuthorizeController Controller { get; private set; }

        public FrmColListAuthorize()
        {
            InitializeComponent();
        }
        public FrmColListAuthorize(int groupid, string groupname,string formtext, int webpartid)
            : this()
        {
            InitializeTreeView();
            this.CurrentGroupID = groupid;
            this.CurrentWebpartID = webpartid;
            this.txtCurrentGroup.Text = groupname;
            this.txtFormText.Text = formtext;
            this.Controller = new ListAuthorizeController(this);
        }
         
        private void InitializeTreeView()
        {
            DevExpress.XtraTreeList.Columns.TreeListColumn treeListColumn1 = new DevExpress.XtraTreeList.Columns.TreeListColumn();
            DevExpress.XtraTreeList.Columns.TreeListColumn treeColMenu = new DevExpress.XtraTreeList.Columns.TreeListColumn();
            DevExpress.XtraTreeList.Columns.TreeListColumn treeColGridName = new DevExpress.XtraTreeList.Columns.TreeListColumn();
            treeListColumn1.Caption = "列ID";
            treeListColumn1.FieldName = "list_id";
            treeListColumn1.Name = "treeListColumn1";

            treeColGridName.Caption = "Grid Name";
            treeColGridName.FieldName = "grid_name";
            treeColGridName.Name = "treeColGridName";
            treeColGridName.Visible = true;
            treeColGridName.VisibleIndex = 1;

            treeColMenu.Caption = "未授权列";
            treeColMenu.FieldName = "list_name";
            treeColMenu.MinWidth = 49;
            treeColMenu.Name = "treeColMenu";
            treeColMenu.OptionsColumn.AllowEdit = false;
            treeColMenu.OptionsColumn.AllowMove = false;
            treeColMenu.OptionsColumn.AllowMoveToCustomizationForm = false;
            treeColMenu.OptionsColumn.ReadOnly = true;
            treeColMenu.OptionsColumn.ShowInCustomizationForm = false;
            treeColMenu.RowFooterSummaryStrFormat = "";
            treeColMenu.SummaryFooter = DevExpress.XtraTreeList.SummaryItemType.Count;
            treeColMenu.SummaryFooterStrFormat = "未授权列数：{0}";
            treeColMenu.Visible = true;
            treeColMenu.VisibleIndex = 0;
            treeUnAuth.treeView.Columns.AddRange(new DevExpress.XtraTreeList.Columns.TreeListColumn[] {
            treeListColumn1,
            treeColMenu,treeColGridName});

            DevExpress.XtraTreeList.Columns.TreeListColumn treeListColumn1Au = new DevExpress.XtraTreeList.Columns.TreeListColumn();
            DevExpress.XtraTreeList.Columns.TreeListColumn treeColMenuAu = new DevExpress.XtraTreeList.Columns.TreeListColumn();
            DevExpress.XtraTreeList.Columns.TreeListColumn treeColGridNameUn = new DevExpress.XtraTreeList.Columns.TreeListColumn();
            treeColGridNameUn.Caption = "Grid Name";
            treeColGridNameUn.FieldName = "grid_name";
            treeColGridNameUn.Name = "treeColGridNameUn";
            treeColGridNameUn.Visible = true;
            treeColGridNameUn.VisibleIndex = 1;

            treeListColumn1Au.Caption = "列ID";
            treeListColumn1Au.FieldName = "list_id";
            treeListColumn1Au.Name = "treeListColumn1Au";

            treeColMenuAu.Caption = "授权列";
            treeColMenuAu.FieldName = "list_name";
            treeColMenuAu.MinWidth = 49;
            treeColMenuAu.Name = "treeColMenuAu";
            treeColMenuAu.OptionsColumn.AllowEdit = false;
            treeColMenuAu.OptionsColumn.AllowMove = false;
            treeColMenuAu.OptionsColumn.ReadOnly = true;
            treeColMenuAu.SummaryFooter = DevExpress.XtraTreeList.SummaryItemType.Count;
            treeColMenuAu.SummaryFooterStrFormat = "授权列数：{0}";
            treeColMenuAu.Visible = true;
            treeColMenuAu.VisibleIndex = 0;
            treeColMenuAu.OptionsColumn.AllowSize = true;
            treeAuth.treeView.Columns.AddRange(new DevExpress.XtraTreeList.Columns.TreeListColumn[] {
            treeListColumn1Au,
            treeColMenuAu,treeColGridNameUn});
        }

        #region IListAuthorizeView 成员
         
        public void BindUnAuthCols(IList<Column_List> unAuthCols)
        {
            AppendNodes(treeUnAuth.treeView, unAuthCols);
        }

        public void BindAuthCols(IList<Column_List> authCols)
        {
            AppendNodes(treeAuth.treeView, authCols);
        }

        public event EditValueChangedHandler EditValueChanged;

        public event AuthListHandler AuthList;

        public event UnAuthListHandler UnAuthList;

        #endregion

        private void AppendNodes(TreeList treeview, IList<Column_List> cols)
        {
            Cursor currentCursor = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;

            treeview.Nodes.Clear();

            treeview.BeginUpdate();
            TreeListNode node;
            foreach (CP.Entities.Column_List col in cols)
            {
                node = treeview.AppendNode((new object[] { col.list_id, col.list_name,col.grid_name }), -1, -1, -1, 3);
                node.Tag = col.list_id;
            }
            treeview.EndUpdate();

            Cursor.Current = currentCursor;
        }

        private void GetCheckedNodes(TreeListNodes nodes, IList<int> checkednodes)
        {
            foreach (TreeListNode node in nodes)
            {
                if (node.CheckState == CheckState.Checked)
                    checkednodes.Add(Convert.ToInt32(node.Tag));
            }
        }

        private void btnAuth_Click(object sender, EventArgs e)
        {
            IList<int> cols = new List<int>();
            GetCheckedNodes(treeUnAuth.treeView.Nodes, cols);
            if (null != AuthList)
            {
                try
                {
                    AuthList(CurrentGroupID, CurrentWebpartID, cols);
                    LogHelper.Log("列授权", "授权", "CurrentGroupID:" +
                        CurrentGroupID.ToString(), "CurrentMenuID:" +
                        CurrentWebpartID.ToString(), "btns:" + string.Join(",", cols));
                    XtraMessageBox.Show("提示信息：按钮授权成功");
                    RefreshColumns();
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show("提示信息：操作错误，错误信息：" + ex.Message);
                }
            }
        }

        private void btnCancelAuth_Click(object sender, EventArgs e)
        {
            IList<int> cols = new List<int>();
            GetCheckedNodes(treeAuth.treeView.Nodes, cols);

            if (null != UnAuthList)
            {
                try
                {
                    UnAuthList(CurrentGroupID, CurrentWebpartID, cols);
                    LogHelper.Log("列授权", "取消授权", "CurrentGroupID:" +
                       CurrentGroupID.ToString(), "CurrentMenuID:" +
                       CurrentWebpartID.ToString(), "btns:" + string.Join(",", cols));
                    XtraMessageBox.Show("提示信息：取消按钮授权成功");
                    RefreshColumns();
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show("提示信息：操作错误，错误信息：" + ex.Message);
                }
            }
        }

        private void RefreshColumns()
        {
            if (null != EditValueChanged)
                EditValueChanged(CurrentGroupID, CurrentWebpartID);
        }


        private void FrmColListAuthorize_Load(object sender, EventArgs e)
        {
            RefreshColumns();
        }

    }
}
